﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BankCreditPortfolio.Core.Helpers.MenuHelper;
using BankCreditPortfolio.DAL.Services;

namespace BankCreditPortfolio.Controllers
{
    //[RequireHttps]
    [ValidateInput(false)]
    public class BaseController : Controller
    {
        protected override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            if (filterContext.HttpContext.User.Identity.IsAuthenticated)
            {
                var accountService = new AccountService();
                var userEmail = filterContext.HttpContext.User.Identity.Name;
                var role = accountService.GetRoleByEmail(userEmail).Title;

                var menu = BuildMenu(role);
                if (menu != null)
                {
                    ViewData["Menu"] = menu;
                }
            }
        }

        private Menu BuildMenu(string role)
        {
            if (!string.IsNullOrEmpty(role))
            {
                switch (role)
                {
                    case "Client": //make enum!
                        return BuildClientMenu();
                    case "CreditCommitteeMember": //make enum!
                        return BuildCreditCommitteeMemberMenu();
                    default: // "OperationsAnalyst", "SafetyOfficer", "CustomerEngineer"
                        return BuildBankerMenu();
                }
            }
            return null;
        }

        private Menu BuildClientMenu()
        {
            var menu = new Menu();

            menu.MainMenuItems = new List<MainMenuItem>()
            {
                new MainMenuItem("Home", string.Empty, string.Empty, new List<SubMenuItem>() {
                    new SubMenuItem("Available Tarrifs", "Tariffs", "Home"),
                }),
                new MainMenuItem("Client Cabinet", string.Empty, string.Empty, new List<SubMenuItem>() {
                    new SubMenuItem("Details", "ChangeDetails", "ClientCabinet"),
                    new SubMenuItem("Credits", "Credits", "ClientCabinet"),
                    new SubMenuItem("Change Password", "ChangePassword", "Account")
                }),
                new MainMenuItem("Orders", string.Empty, string.Empty, new List<SubMenuItem>() {
                    new SubMenuItem("New Order", "DrawOrder", "ClientCabinet"),
                    new SubMenuItem("My Orders", "Orders", "ClientCabinet")
                })
            };

            return menu;
        }

        private Menu BuildBankerMenu()
        {
            var menu = new Menu();

            menu.MainMenuItems = new List<MainMenuItem>()
            {
                new MainMenuItem("Home", string.Empty, string.Empty, new List<SubMenuItem>() {
                    new SubMenuItem("Available Tarrifs", "Tariffs", "Home"),
                }),
                new MainMenuItem("Orders", string.Empty, string.Empty, new List<SubMenuItem>() {
                    new SubMenuItem("Orders For Review", "Orders", "Bank"),
                }),
                new MainMenuItem("Statistics", string.Empty, string.Empty, new List<SubMenuItem>() {
                    new SubMenuItem("Credits", "Credits", "Bank"),
                    new SubMenuItem("Archive of orders", "OrdersArchive", "Bank"),
                })
            };

            return menu;
        }

        private Menu BuildCreditCommitteeMemberMenu()
        {
            var menu = new Menu();

            menu.MainMenuItems = new List<MainMenuItem>()
            {
                new MainMenuItem("Home", string.Empty, string.Empty, new List<SubMenuItem>() {
                    new SubMenuItem("Available Tarrifs", "Tariffs", "Home"),
                }),
                new MainMenuItem("Orders", string.Empty, string.Empty, new List<SubMenuItem>() {
                    new SubMenuItem("Orders For Review", "Orders", "Bank"),
                }),
                new MainMenuItem("Statistics", string.Empty, string.Empty, new List<SubMenuItem>() {
                    new SubMenuItem("Payments", "Payments", "Bank"),
                    new SubMenuItem("Credits", "Credits", "Bank"),
                    new SubMenuItem("State Of Deposits", "BankDeposits", "Bank"),
                    new SubMenuItem("Archive of orders", "OrdersArchive", "Bank"),
                })
            };

            return menu;
        }
    }
}
